<template>
	<el-card>
		<div class="todeoDivClass" style="height: calc(100vh - 260px)">
			<el-form :inline="true" :model="state.queryForm">
				<el-form-item>
					<el-input v-model="state.queryForm.pttNm" placeholder="请输入姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="state.queryForm.slfTelNo" placeholder="请输入手机号"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="state.queryForm.idCrdNo" placeholder="请输入证件号"></el-input>
				</el-form-item>
				<el-form-item>
					<fast-select
						style="width: 130px"
						v-model="state.queryForm.gdrCd"
						dict-type="mpi_gdr_cd"
						placeholder="请选择性别"
						clearable
						filterable
					></fast-select>
				</el-form-item>
				<!-- <el-form-item>
					<el-date-picker v-model="dateTime" type="datetimerange" start-placeholder="开始时间" end-placeholder="结束时间"
						style="width: 300px" format="YYYY-MM-DD HH:mm:ss" time-format="HH:mm:ss" @change="dateTimeChange" />
				</el-form-item> -->
				<!-- <el-form-item>
					<el-date-picker
						v-model="state.queryForm.startDate"
						type="datetime"
						placeholder="开始时间"
						format="YYYY-MM-DD HH:mm"
						time-format="HH:mm"
						@change="dataSTChange"
					/>
				</el-form-item> -->
				<!-- <el-form-item>
					<el-date-picker
						v-model="state.queryForm.endDate"
						type="datetime"
						placeholder="结束时间"
						format="YYYY-MM-DD HH:mm"
						time-format="HH:mm"
						@change="dataETChange"
					/>
				</el-form-item> -->

				<el-form-item>
					<el-button :icon="Search" @click="searchBtn" type="primary">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button :icon="Refresh" @click="resets">重置</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="danger" v-auth="'patient-primary:abnormal-data:ignore'" :icon="Delete" @click="ignores">批量忽略</el-button>
				</el-form-item>
			</el-form>
			<el-table
				v-loading="state.dataListLoading"
				:data="state.dataList"
				border
				max-height="calc(100vh - 410px )"
				style="width: 100%"
				@selection-change="selectionChangeHandle"
			>
				<el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
				<el-table-column prop="prmKey" label="主键ID" width="150" align="center">
					<template #default="scope">
						<span @click="getDetailes(scope.row.prmKey)" style="color: #5774f4; cursor: pointer">
							{{ scope.row.prmKey }}
						</span>
					</template>
				</el-table-column>
				<el-table-column prop="pttNm" label="姓名" header-align="center" align="center"></el-table-column>
				<fast-table-column prop="gdrCd" label="性别" dict-type="mpi_gdr_cd" header-align="center" align="center"></fast-table-column>
				<el-table-column prop="bthDt" label="出生日期" header-align="center" align="center"></el-table-column>
				<el-table-column prop="slfTelNo" label="手机号" header-align="center" align="center"></el-table-column>
				<el-table-column prop="address" label="现住址" header-align="center" align="center"></el-table-column>
				<el-table-column prop="idCrdNo" label="证件号" header-align="center" align="center" width="180"></el-table-column>
				<el-table-column prop="createTime" label="创建时间" header-align="center" align="center"></el-table-column>
				<!-- <el-table-column prop="score" label="分值" header-align="center" align="center"></el-table-column> -->
				<el-table-column label="操作" fixed="right" header-align="center" align="center" width="200">
					<template #default="scope">
						<el-button type="primary" link @click="getDetailes(scope.row.prmKey)">查看</el-button>
						<el-button type="primary" v-auth="'patient-primary:abnormal-data:handle'" link @click="addOrUpdateHandle(scope.row, 'edit')"
							>处理</el-button
						>
						<el-button type="danger" v-auth="'patient-primary:abnormal-data:ignore'" link @click="ignore(scope.row)">忽略</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				:current-page="state.page"
				:page-sizes="state.pageSizes"
				:page-size="state.limit"
				:total="state.total"
				layout="total, sizes, prev, pager, next, jumper"
				@size-change="sizeChangeHandle"
				@current-change="currentChangeHandle"
			>
			</el-pagination>
			<!-- 弹窗, 新增 -->
			<div class="todeClass" style="height: 100%" v-if="todoHandleVisiable">
				<todo-handle ref="todoHandleRef" v-if="todoHandleVisiable" @close="closeTodo"></todo-handle>
			</div>

			<el-drawer style="padding: 0" v-model="patientVisible" :show-close="false" :destroy-on-close="true" size="70%">
				<template #header="{ titleId, titleClass }">
					<span :id="titleId" :class="titleClass"> 当前患者数据 </span>
				</template>
				<div style="font-weight: 600; line-height: 30px; background: #f7f8fa; color: #1d2129; padding-left: 16px; margin: 10px 0">患者基本信息</div>
				<el-form disabled ref="dataFormRef" :model="dataForm" label-width="100px" class="patient-form">
					<el-row>
						<el-col :span="8">
							<el-form-item prop="pttNm" label="姓名" :rules="{ required: true }">
								<el-input v-model="dataForm.pttNm" style="width: 100%" placeholder="请输入姓名"></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item prop="gdrCd" label="性别" style="width: 100%">
								<fast-select
									v-model="dataForm.gdrCd"
									style="width: 100%"
									dict-type="mpi_gdr_cd"
									placeholder="请选择性别"
									clearable
									filterable
								></fast-select>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item prop="age" label="年龄">
								<el-input style="width: 100%" v-model="dataForm.age" disabled placeholder="请输入年龄"></el-input>
							</el-form-item>
						</el-col>
					</el-row>

					<el-row>
						<el-col :span="8">
							<el-form-item prop="idCard" label="证件类型" style="width: 100%">
								<fast-select v-model="dataForm.idCrdCgyCd" style="width: 100%" dict-type="mpi_id_crd_cgy_cd" clearable filterable></fast-select>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item prop="idCrdNo" label="证件号" :rules="{ required: true }">
								<el-input disabled style="width: 100%" v-model="dataForm.idCrdNo" placeholder="请输入证件号"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="8">
							<el-form-item prop="slfTelNo" label="联系电话">
								<el-input v-model="dataForm.slfTelNo" style="width: 100%" placeholder="请输入联系电话"></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item prop="aboCd" label="ABO血型">
								<fast-select
									v-model="dataForm.aboCd"
									style="width: 100%"
									dict-type="mpi_abo_cd"
									placeholder="请选择ABO血型"
									clearable
									filterable
								></fast-select>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item prop="rhCd" label="Rh血型">
								<fast-select
									v-model="dataForm.rhCd"
									style="width: 100%"
									dict-type="mpi_rh_cd"
									placeholder="请选择Rh血型"
									clearable
									filterable
								></fast-select>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="8">
							<el-form-item prop="ntnlyCd" label="国籍">
								<fast-select v-model="dataForm.ntnlyCd" style="width: 100%" dict-type="mpi_ntnly_cd" placeholder="请选择国籍" clearable filterable>
								</fast-select>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item prop="ntnCd" label="民族">
								<fast-select
									v-model="dataForm.ntnCd"
									style="width: 100%"
									dict-type="mpi_ntn_cd"
									placeholder="请选择民族"
									clearable
									filterable
								></fast-select>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item prop="bthDt" label="出生日期">
								<el-input disabled v-model="dataForm.bthDt" style="width: 100%" placeholder="请选择出生日期"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="8">
							<el-form-item prop="mryCdtCd" label="婚姻状况">
								<fast-select
									v-model="dataForm.mryCdtCd"
									style="width: 100%"
									placeholder="请选择婚姻状况"
									dict-type="mpi_mry_cdt_cd"
									clearable
									filterable
								>
								</fast-select>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item prop="eduCd" label="学历">
								<fast-select
									v-model="dataForm.eduCd"
									style="width: 100%"
									placeholder="请选择学历"
									dict-type="mpi_edu_cd"
									clearable
									filterable
								></fast-select>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="8">
							<el-form-item prop="age" label="单位名称">
								<el-input v-model="dataForm.wrkUntNm" style="width: 100%" placeholder="请输入工作单位名称"></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item prop="age" label="单位电话">
								<el-input v-model="dataForm.wrkUntTelNo" style="width: 100%" placeholder="请输入工作单位电话"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="8">
							<el-form-item prop="mdcrCgyCd" label="医疗支付方式">
								<fast-select
									v-model="dataForm.mdcrCgyCd"
									style="width: 100%"
									placeholder="请选择医疗支付方式"
									dict-type="mpi_mdcr_cgy_cd"
									clearable
									filterable
								>
								</fast-select>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item prop="pfsCgyCd" label="职业类别">
								<fast-select
									v-model="dataForm.pfsCgyCd"
									style="width: 100%"
									placeholder="请选择职业类别"
									dict-type="mpi_pfs_cgy_cd"
									clearable
									filterable
								>
								</fast-select>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="8">
							<el-form-item prop="dthFlg" label="本人死亡标识">
								<el-radio-group v-model="dataForm.dthFlg" style="width: 100%">
									<el-radio :label="0">未死亡</el-radio>
									<el-radio :label="1">已死亡</el-radio>
								</el-radio-group>
							</el-form-item>
						</el-col>
						<el-col :span="8"></el-col>
						<el-col :span="8"></el-col>
					</el-row>
				</el-form>
				<div style="font-weight: 600; line-height: 30px; background: #f7f8fa; color: #1d2129; padding-left: 16px; margin-bottom: 10px">其他信息</div>
				<el-tabs class="patient-info-tabs" v-model="activeName" type="border-card">
					<el-tab-pane label="地址信息" name="1">
						<el-table :data="dataForm.dwdAddrVO" style="width: 100%" max-height="250" size="small">
							<el-table-column prop="addrType" label="地址信息" headerAlign="left" align="left" />
							<el-table-column prop="addr" label="地址信息" headerAlign="left" align="left" />
							<el-table-column prop="postCd" width="120" label="邮编" headerAlign="left" align="left" />
						</el-table>
					</el-tab-pane>
					<el-tab-pane label="卡信息" name="2">
						<el-table :data="dataForm.dwdCardInfoVO" style="width: 100%" max-height="250" size="small">
							<el-table-column prop="cardType" label="卡类型" headerAlign="left" align="left" />
							<el-table-column prop="cardNo" label="卡号" headerAlign="left" align="left" />
						</el-table>
					</el-tab-pane>
					<el-tab-pane label="证件信息" name="3">
						<el-table :data="dataForm.dwdIdCardInfoVO" style="width: 100%" max-height="250" size="small">
							<fast-table-column prop="idCrdCgyCd" label="证件类型" dict-type="mpi_id_crd_cgy_cd" headerAlign="left" align="left"></fast-table-column>
							<el-table-column prop="idCrdNo" label="证件号" headerAlign="left" align="left" />
						</el-table>
					</el-tab-pane>
					<el-tab-pane label="联系人信息" name="4">
						<el-table :data="dataForm.dwdContactInfoVO" style="width: 100%" max-height="250" size="small">
							<fast-table-column prop="ctctRlt" label="与本人关系" dict-type="mpi_ctct_rlt_tp_cd" headerAlign="left" align="left"></fast-table-column>
							<el-table-column prop="ctctNm" label="联系人" headerAlign="left" align="left" />
							<el-table-column prop="ctctTelNo" label="联系方式" headerAlign="left" align="left" />
						</el-table>
					</el-tab-pane>
					<el-tab-pane label="建档信息" name="5">
						<el-table :data="[]" style="width: 100%" max-height="250" size="small">
							<el-table-column prop="crtRcdOrgNm" label="建档医疗机构" headerAlign="left" align="left" />
							<el-table-column prop="crtRcdDctNm" label="建档医生" headerAlign="left" align="left" />
							<el-table-column prop="crtRcdDt" label="建档日期" headerAlign="left" align="left" />
							<el-table-column prop="crtRcdStrtDt" label="建档生效日期" headerAlign="left" align="left" />
							<el-table-column prop="rcdSttsCd" label="建档状态" headerAlign="left" align="left">
								<template #default="scope">
									{{ scope.row.rcdSttsCd == '0' ? '正常' : '异常' }}
								</template>
							</el-table-column>
						</el-table>
					</el-tab-pane>
					<el-tab-pane label="签约信息" name="6">
						<el-table :data="[]" style="width: 100%" max-height="250" size="small">
							<el-table-column prop="sgnCtrctOrgNm" label="签约医疗机构" headerAlign="left" align="left" />
							<el-table-column prop="sgnCtrctDctNm" label="签约医生" headerAlign="left" align="left" />
							<el-table-column prop="sgnCtrctDt" label="签约日期" headerAlign="left" align="left" />
							<el-table-column prop="sgnCtrctStrtDt" label="签约生效日期" headerAlign="left" align="left" />
							<el-table-column prop="sgnCtrctExprDt" label="签约到期日期" headerAlign="left" align="left" />
							<el-table-column prop="sgnCtrctSttsCd" label="签约状态" headerAlign="left" align="left">
								<template #default="scope">
									{{ scope.row.sgnCtrctSttsCd == '0' ? '正常' : '异常' }}
								</template>
							</el-table-column>
						</el-table>
					</el-tab-pane>
				</el-tabs>
			</el-drawer>
		</div>
	</el-card>
</template>

<script setup lang="ts">
import { useCrud } from '@/hooks/indexZSYPost.ts'
import { nextTick, reactive, ref } from 'vue'
import { IHooksOptions } from '@/hooks/interface'

import { ElMessage, ElMessageBox } from 'element-plus/es'
import { Delete, Edit, Search, Plus, CircleClose, Refresh } from '@element-plus/icons-vue'
import { ElLoading } from 'element-plus'

import { exceptionList } from '@/api/patient-primary/adnormal-data'
import TodoHandle from './todo-handle.vue'
import timeFormater from 'time-formater'
import { ignoreExceptionData, getExceptionData } from '@/api/patient-primary/todo-handle'

const dateTime = ref([])
const todoHandleRef = ref([])

const todoHandleVisiable = ref(false)

const state: IHooksOptions = reactive({
	dataListUrl: '/empi/empiabnormalrecord/page',
	queryForm: {
		pttNm: '',
		slfTelNo: '',
		idCrdNo: '',
		gdrCd: '',
		// startDate: '',
		// endDate: '',
		margeStatus: 0,
		dateType: 'createTime'
	},
	dataListSelections: []
})

const dataForm = reactive({
	idCard: '',
	currAddrDscpt: '',
	pttNm: '',
	gdrCd: '',
	idCrdNo: '',
	slfTelNo: '',
	aboCd: '',
	rhCd: '',
	ntnlyCd: '',
	ntnCd: '',
	bthDt: '',
	mryCdtCd: '',
	eduCd: '',
	wrkUntNm: '',
	wrkUntTelNo: '',
	mdcrCgyCd: '',
	pfsCgyCd: '',
	joinWrkDt: '',
	age: '',
	dwdCardInfoVO: [],
	dwdContactInfoVO: [],
	dwdIdCardInfoVO: [],
	dwdAddrVO: []
})

// const dateTimeChange = e => {
// 	dateTime.value = e
// 	if (e) {
// 		state.queryForm.startDate = timeFormater(e[0]).format('YYYY-MM-DD HH:mm')
// 		state.queryForm.endDate = timeFormater(e[1]).format('YYYY-MM-DD HH:mm')
// 	} else {
// 		state.queryForm.startDate = ''
// 		state.queryForm.endDate = ''
// 	}
// }

const addOrUpdateHandle = (data, type) => {
	todoHandleVisiable.value = true
	nextTick(() => {
		todoHandleRef.value.init(data, type)
	})
}

const searchBtn = () => {
	state.page = 1
	getDataList()
}

const resets = () => {
	state.queryForm = {
		pttNm: '',
		slfTelNo: '',
		idCrdNo: '',
		gdrCd: '',
		// startDate: '',
		// endDate: '',
		margeStatus: 0,
		dateType: 'createTime'
	}
	// dateTime.value = []

	state.dataListSelections = []

	state.page = 1
	state.limit = 10

	todoHandleVisiable.value = false

	getDataList()
}

const closeTodo = () => {
	todoHandleVisiable.value = false
	getDataList()
}

// 忽略
const ignore = data => {
	ElMessageBox.confirm(`确认忽略主键ID【${data.prmKey}】的数据么？`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning'
	})
		.then(() => {
			ignoreExceptionData([data.abnormalRecordId]).then(() => {
				ElMessage.success('忽略成功')
				getDataList()
			})
		})
		.catch(error => {})
}

// 批量忽略
const ignores = () => {
	if (state.dataListSelections.length) {
		const data = state.dataList?.filter(item => state.dataListSelections?.some(ele => ele === item.id))

		let name = ''
		const ids: any = []
		data?.forEach((item, index) => {
			ids.push(item.abnormalRecordId)
			if (index) {
				name = name + ',' + item.prmKey
			} else {
				name = item.prmKey
			}
		})

		ElMessageBox.confirm(`确认忽略主键ID【${name}】的数据么？`, '提示', {
			confirmButtonText: '确定',
			cancelButtonText: '取消',
			type: 'warning'
		})
			.then(() => {
				ignoreExceptionData(ids).then(() => {
					ElMessage.success('忽略成功')
					getDataList()
				})
			})
			.catch(error => {})
	} else {
		ElMessage.warning('请选择需忽略的数据')
	}
}
const patientVisible = ref(false)
const activeName = ref('1')
const getDetailes = id => {
	patientVisible.value = true
	activeName.value = '1'
	getData(id)
}

const getData = id => {
	getExceptionData({ prmKey: id }).then(res => {
		Object.assign(dataForm, res.data)

		dataForm.dwdCardInfoVO = []
		dataForm.dwdContactInfoVO = []
		dataForm.dwdIdCardInfoVO = []
		dataForm.dwdAddrVO = []
		const dwdCardInfoVO = res.data.dwdCardInfoVO
		const dwdContactInfoVO = res.data.dwdContactInfoVO
		const dwdIdCardInfoVO = res.data.dwdIdCardInfoVO
		const dwdAddrVO = res.data.dwdAddrVO
		if (dwdCardInfoVO) {
			if (dwdCardInfoVO.uarrHlthRcdNo) {
				dataForm.dwdCardInfoVO.push({ cardType: '城乡居民健康档案', cardNo: dwdCardInfoVO.uarrHlthRcdNo })
			}
			if (dwdCardInfoVO.scstCrdNo) {
				dataForm.dwdCardInfoVO.push({ cardType: '社保卡', cardNo: dwdCardInfoVO.scstCrdNo })
			}
			if (dwdCardInfoVO.hlthCrdNo) {
				dataForm.dwdCardInfoVO.push({ cardType: '居民健康卡', cardNo: dwdCardInfoVO.hlthCrdNo })
			}
			if (dwdCardInfoVO.mdcrCrdNo) {
				dataForm.dwdCardInfoVO.push({ cardType: '医保卡', cardNo: dwdCardInfoVO.mdcrCrdNo })
			}
			if (dwdCardInfoVO.vstCrdNo) {
				dataForm.dwdCardInfoVO.push({ cardType: '就诊卡', cardNo: dwdCardInfoVO.vstCrdNo })
			}
		}
		if (dwdContactInfoVO) {
			if (dwdContactInfoVO.ctctNm || dwdContactInfoVO.ctctRlt || dwdContactInfoVO.ctctTelNo) {
				dataForm.dwdContactInfoVO.push(dwdContactInfoVO)
			}
		}
		if (dwdIdCardInfoVO) {
			if (dwdIdCardInfoVO.idCardNo) {
				dataForm.dwdIdCardInfoVO.push(dwdIdCardInfoVO)
			}
		}

		if (dwdAddrVO) {
			if (dwdAddrVO.currAddr || dwdAddrVO.currAddrPostCd) {
				dataForm.dwdAddrVO.push({ addrType: '现住址', addr: dwdAddrVO.currAddr, postCd: dwdAddrVO.currAddrPostCd })
			}
			if (dwdAddrVO.wrkUntAddr || dwdAddrVO.wrkUntAddrPostCd) {
				dataForm.dwdAddrVO.push({ addrType: '工作单位地址', addr: dwdAddrVO.wrkUntAddr, postCd: dwdAddrVO.wrkUntAddrPostCd })
			}
			if (dwdAddrVO.bthAddr) {
				dataForm.dwdAddrVO.push({ addrType: '出生地址', addr: dwdAddrVO.bthAddr, postCd: '' })
			}
			if (dwdAddrVO.ntvPlcAddr) {
				dataForm.dwdAddrVO.push({ addrType: '籍贯地址', addr: dwdAddrVO.ntvPlcAddr, postCd: '' })
			}
			if (dwdAddrVO.ctctAddr) {
				dataForm.dwdAddrVO.push({ addrType: '联系人地址', addr: dwdAddrVO.ctctAddr, postCd: '' })
			}
		}

		dataForm.age = bthDtToAge(dataForm.bthDt).age
	})
}

// const dataSTChange = e => {
// 	if (e) {
// 		if (state.queryForm.endDate && new Date(e).getTime() > new Date(state.queryForm.endDate).getTime()) {
// 			ElMessage.warning('开始时间不能大于结束时间，请重新选择时间')
// 			state.queryForm.startDate = ''
// 			return
// 		}
// 		state.queryForm.startDate = timeFormater(e).format('YYYY-MM-DD HH:mm:ss')
// 	} else {
// 		state.queryForm.startDate = ''
// 	}
// }
// const dataETChange = e => {
// 	if (e) {
// 		if (state.queryForm.startDate && new Date(e).getTime() < new Date(state.queryForm.startDate).getTime()) {
// 			ElMessage.warning('结束时间不能小于开始时间，请重新选择时间')
// 			state.queryForm.endDate = ''
// 			return
// 		}
// 		state.queryForm.endDate = timeFormater(e).format('YYYY-MM-DD HH:mm:ss')
// 	} else {
// 		state.queryForm.endDate = ''
// 	}
// }

const bthDtToAge = val => {
	if (val) {
		let currentYear = new Date().getFullYear() //当前的年份
		let calculationYear = new Date(val).getFullYear() //计算的年份
		const wholeTime = currentYear + val.substring(4) //周岁时间
		const calculationAge = currentYear - calculationYear //按照年份计算的年龄
		//判断是否过了生日
		if (new Date().getTime() > new Date(wholeTime).getTime()) {
			return { age: calculationAge }
		} else {
			return { age: calculationAge - 1 }
		}
	} else {
		return { age: '' }
	}
}

const { getDataList, selectionChangeHandle, sizeChangeHandle, currentChangeHandle, deleteHandle } = useCrud(state)

defineExpose({
	resets
})
</script>

<style lang="scss">
.todeoDivClass {
	height: calc(100vh - 170px);
	position: relative;
	overflow: hidden;
}

.todeoDivClass .todeClass .el-overlay {
	height: 100%;
	position: absolute !important;
	overflow: hidden;
}
</style>
